﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.Web.Formly
{
    [Serializable]
    public class ExpressionProperty
    {
        private object m_callback = null;

        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        /// <value>
        /// The name of the property.
        /// </value>
        public string PropertyName { get; set; }

        /// <summary>
        /// Gets or sets the expression callback.
        /// </summary>
        /// <value>
        /// The expression callback.
        /// </value>
        /// <remarks>
        ///   The expession callback defines the behavior associated to the properties.
        ///   
        ///   The behavior ca be defined by a simple statement: 
        ///  'templateOptions.foo': '$modelValue', // set to the $modelValue of the control
        ///  
        ///   Or can be defined by a function...
        ///   'hide': function($viewValue, $modelValue, scope) {
        ///             return scope.model.baz === 'foobar';
        ///           }
        /// </remarks>
        public object ExpressionCallback 
        {
            get 
            {
                object m_func = null;
                string m_strFunc = string.Empty;

                m_strFunc = string.Format(@"function($viewValue, $modelValue, scope){{ return {0}($viewValue, $modelValue, scope); }}", m_callback);
                m_func = new Newtonsoft.Json.Linq.JRaw(m_strFunc);

                return m_func; 
            }
            set 
            {
                m_callback = value;
            }
        }

        public ExpressionProperty()
        {

        }

        public ExpressionProperty(string name, object callback)
        {
            this.PropertyName = name;
            this.ExpressionCallback = callback;
        }
    }
}
